<?php
/**
 * Created by PhpStorm.
 * User: alice
 * Date: 2015/12/28
 * Time: 14:51
 */
/**
 * Class ProductService
 */
class GoodsService extends BaseService
{
    private static $self = NULL;

    static public function instance()
    {
        if (self::$self == NULL) {
            self::$self = new self;
        }

        return self::$self;
    }

    private $mProductCate                       = NULL;
    private $mProductBrand                      = NULL;
    private $mProduct                           = NULL;
    private $mProducingArea                     = NULL;
    private $mSpec                              = NULL;
    private $mLabel                             = NULL;



    public function __construct(){

        $this->mProductCate                     = new Involved_ProductCateModel();
        $this->mProductBrand                    = new Involved_ProductBrandModel();
        $this->mProduct                         = new Involved_ProductModel();
        $this->mLevel                           = new Involved_LevelModel();
        $this->mProducingArea                   = new Involved_ProducingAreaModel();
        $this->mSpec                            = new Involved_SpecModel();
        $this->mLabel                           = new Involved_LabelModel();
    }

    /**
     * 参数验证
     * @param $obj
     * @param $arr
     * @author alice
     * @date 2016-1-13
     * @return bool
     */
    public function  checkProperties($obj, $arr)
    {
        if(empty($arr)) return true;
        $obj    =   (array) $obj;
        foreach($arr as $v){
            if(empty($obj[$v])){
                return false;}
        }
        return true;
    }

    /**
     * 产品分类列表
     * return productList
     */
    public function GetGoodsCateList()
    {
        return $this->mProductCate->GetGoodsCateList();
    }

    public function GetProductCateList()
    {
        return $this->mProductCate->GetProductCateList();
    }

    /**
     * 产品分类信息
     * return productOne
     */
    public function GetGoodsCateInfo($cate_id)
    {
         return $this->mProductCate->GetGoodsCateInfo($cate_id);

    }

    /**
     * 新增产品分类
     * @param $paramObj
     * @author alice
     * @date 2016-1-18
     * @return int
     * @throws Exception
     */
    public function AddGoodsCate($paramObj)
    {
        $check =  $this->checkProperties($paramObj,['brand_name']);
        if (!$check) {
            throw new Exception($this->error_msg[ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST],
                ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST);
        }

        #添加分类
        $aData = [
            'cate_name'          =>     $paramObj->cate_name,
            'parent_id'          =>     $paramObj->parent_id,
            'length'             =>     $paramObj->length,
            'description'        =>     $paramObj->description,
        ];
        return $this->mProductCate->insert($aData);

    }

    /**
     * 修改产品分类
     * @param $paramObj
     * @author alice
     * @date 2016-1-18
     * @return int
     */
    public function EditGoodsCate($paramObj)
    {
        #修改分类
        $aData = [
            'cate_name'          =>     $paramObj->cate_name,
            'parent_id'          =>     $paramObj->parent_id,
            'length'             =>     $paramObj->length,
            'description'        =>     $paramObj->description,
        ];
        return $this->mProductCate->update($aData,['cate_id'=>$paramObj->cate_id]);

    }

    /**
     * 产品品牌列表
     * @author alice
     * @date 2016-1-18
     * @return mixed
     */
    public function ShowGoodsBrandList()
    {
        return $this->mProductBrand->ShowGoodsBrandList();

    }

    /**
     * 新增产品品牌
     * @param $paramObj
     * @author alice
     * @date 2016-1-16
     * @return int
     * @throws Exception
     */
    public function AddGoodsBrand($paramObj)
    {
        $check =  $this->checkProperties($paramObj,['brand_name']);
        if (!$check) {
            throw new Exception($this->error_msg[ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST],
                ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST);
        }
        $aData = [
            'brand_name'          =>     $paramObj->brand_name,
            'start_time'          =>     $paramObj->start_time,
            'brand_img'           =>     $paramObj->brand_img,
            'created'             =>     time(),
        ];
        return $this->mProductBrand->insert($aData);
    }

    /**
     * 产品品牌详情
     * @param $id
     * @author alice
     * @date 2016-1-18
     * @return mixed
     */
    public function ShowGoodsBrandInfo($id)
    {
        return $this->mProductBrand->ShowGoodsBrandInfo($id);
    }

    /**
     * 修改产品品牌
     * @param $paramObj
     * @author alice
     * @date 2016-1-18
     * @return bool
     */
    public function EditGoodsBrand($paramObj)
    {
        $aData = [
            'brand_name'          =>     $paramObj->brand_name,
            'start_time'          =>     $paramObj->start_time,
            'brand_img'           =>     $paramObj->brand_img,
            'created'             =>     time(),
        ];
        return $this->mProductCate->update($aData,['brand_id',$paramObj->brand_id]);

    }

    /**
     * 产品列表
     * @author alice
     * @date 2016-1-18
     * @return mixed
     */
    public function ShowGoodsList()
    {
        return $this->mProduct->ShowGoodsList();

    }

    /**
     * 产品详情
     * @param $id
     * @author alice
     * @date 2016-1-18
     * @return mixed
     */
    public function GetGoodsInfo($id)
    {
        return $this->mProduct->GetGoodsInfo($id);

    }

    /**
     * 新增产品
     * @param $paramObj
     * @author alice
     * @date 2016-1-18
     * @return int
     * @throws Exception
     */
    public function AddGoods($paramObj)
    {
        $check =  $this->checkProperties($paramObj,['product_name','product_sn']);
        if(!$check){
            throw new Exception(ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST,
                ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST);}

        #上传图片
        if(!empty($paramObj->goods_img))
        {
            $file       = $paramObj->goods_img;
            $clientName = $file->getClientOriginalName();
            $extension  = $file->getClientOriginalExtension();
            $newName    = md5(date('ymdhis').$clientName).".".$extension;
            $file->move(public_path().'/resource/upload',$newName);

            #生成缩略图
            ThumbnailService::instance()->setImageSize(200, 200);
            $image = file_get_contents(public_path().'/resource/upload/'.$newName);
            ThumbnailService::instance()->loadData($image, 'image/jpeg');
            ThumbnailService::instance()->buildThumb(public_path().'/resource/upload/thumb_'.$newName);

            $aData['goods_img']       = '/resource/upload/' . $newName;
            $aData['goods_thumb']     = '/resource/upload/' . 'thumb_'.$newName;
        }
        #获取材质父id
        $parent_id = $this->mProductCate->GetQuality;

        #获取分类
        $category = $this->mProductCate->GetCategoryById($paramObj->quality);
        dd($category);
        #获取品类
        $classes = $this->mProductCate->GetClassesById($category->cate_id);
        dd($classes);

        $aData = [
            'product_name'              =>     $paramObj->product_name,
            'product_sn'                =>     $paramObj->product_sn,
            'brand_id'                  =>     $paramObj->brand_id,
            'quality'                   =>     $paramObj->quality,
            'category'                  =>     $category->cate_name,
            'classes'                   =>     $classes->cate_name,
            'channel'                   =>     $paramObj->channel,
            'stock'                     =>     $paramObj->stock,
            'shop_price'                =>     $paramObj->shop_price,
            'description'               =>     $paramObj->description,
            'supplier'                  =>     $paramObj->supplier,
            'place'                     =>     $paramObj->place,
            'spec'                      =>     $paramObj->spec,
            'level'                     =>     $paramObj->level,
            'storage'                   =>     $paramObj->storage,
            'start_num'                 =>     $paramObj->start_num,
            'is_self_support'           =>     $paramObj->is_self_support,
            'is_index'                  =>     $paramObj->is_index,
            'is_up'                     =>     $paramObj->is_up,
        ];

        return $this->mProduct->insert($aData);

    }

    /**
     * 修改产品
     * @param $paramObj
     * @author alice
     * @date 2016-1-18
     * @return bool
     */
    public function EditGoods($paramObj)
    {
        #上传图片
        if(empty($paramObj->goods_img))
        {
            $file       = $paramObj->goods_img;
            $clientName = $file->getClientOriginalName();
            $extension  = $file->getClientOriginalExtension();
            $newName    = md5(date('ymdhis').$clientName).".".$extension;
            $file->move(public_path().'/resource/upload',$newName);

            #生成缩略图
            ThumbnailService::instance()->setImageSize(200, 200);
            $image = file_get_contents(public_path().'/resource/upload/'.$newName);
            ThumbnailService::instance()->loadData($image, 'image/jpeg');
            ThumbnailService::instance()->buildThumb(public_path().'/resource/upload/thumb_'.$newName);

            $aData['goods_img']       = '/resource/upload/' . $newName;
            $aData['goods_thumb']     = '/resource/upload/' . 'thumb_'.$newName;
        }

        $aData = [
            'product_name'              =>     $paramObj->product_name,
            'product_sn'                =>     $paramObj->product_sn,
            'brand_id'                  =>     $paramObj->brand_id,
            'cate_id'                   =>     $paramObj->cate_id,
            'channel'                   =>     $paramObj->channel,
            'stock'                     =>     $paramObj->stock,
            'shop_price'                =>     $paramObj->shop_price,
            'description'               =>     $paramObj->description,
            'supplier'                  =>     $paramObj->supplier,
            'place'                     =>     $paramObj->place,
            'spec'                      =>     $paramObj->spec,
            'level'                     =>     $paramObj->level,
            'storage'                   =>     $paramObj->storage,
            'start_num'                 =>     $paramObj->start_num,
            'is_self_support'           =>     $paramObj->is_self_support,
            'is_index'                  =>     $paramObj->is_index,
            'is_up'                     =>     $paramObj->is_up,
        ];

        return $this->mProduct->update($aData,['product_id'=>$paramObj->product_id]);

    }

    /**
     * 获取材质列表
     * @return mixed
     * @author alice
     * @date 2016-1-26
     */
    public function GetQuality()
    {
        return $this->mProductCate->GetQualityList();
    }

    /**
     * 产地列表展示
     * @return mixed
     * @author alice
     * @date 2016-1-25
     */
    public function ShowAreaList()
    {
        return $this->mProducingArea->ShowAreaList();
    }

    /**
     * 产地详情
     * @param $id
     * @return mixed
     * @author alice
     * @date 2016-1-25
     */
    public function GetAreaInfo($id)
    {
        return $this->mProducingArea->GetAreaInfo($id);
    }

    /**
     * 添加产地
     * @param $paramObj
     * @return int
     * @author alice
     * @date 2016-1-25
     */
    public function AddArea($paramObj)
    {
        $aData = [
            'area_name'          =>     $paramObj->area_name,
            'country'            =>     $paramObj->country,
        ];

        return $this->mProducingArea->insert($aData);

    }

    /**
     * 修改产地
     * @param $paramObj
     * @return bool
     * @author alice
     * @date 2016-1-25
     */
    public function EditArea($paramObj)
    {
        $aData = [
            'area_name'              =>     $paramObj->area_name,
            'country'                =>     $paramObj->country,
        ];

        return $this->mProducingArea->update($aData,['area_id'=>$paramObj->area_id]);

    }

    /**
     * 展示标签列表
     * @return mixed
     * @author alice
     * @date 2016-1-25
     */
    public function ShowLabelList()
    {
        return $this->mLabel->ShowLabelList();
    }

    /**
     * 标签详情
     * @param $id
     * @return mixed
     */
    public function GetLabelInfo($id)
    {
        return $this->mLabel->GetLabelInfo($id);
    }

    /**
     * 添加标签
     * @param $paramObj
     * @return int
     * @author alice
     * @date 2016-1-25
     */
    public function AddLabel($paramObj)
    {
        $aData = [
            'label_name'          =>     $paramObj->label_name,
            'quality'             =>     $paramObj->quality,
        ];
        return $this->mLabel->insert($aData);

    }

    /**
     * 修改标签
     * @param $paramObj
     * @return bool
     * @author alice
     * @date 2016-1-25
     */
    public function EditLabel($paramObj)
    {
        $aData = [
            'label_name'              =>     $paramObj->label_name,
            'quality'                 =>     $paramObj->quality,
        ];

        return $this->mLabel->update($aData,['label_id'=>$paramObj->label_id]);
    }

    /**
     * 展示规格列表
     * @return mixed
     * @author alice
     * @date 2016-1-25
     */
    public function ShowSpecList()
    {
        return $this->mSpec->ShowSpecList();
    }

    /**
     * 规格信息
     * @param $id
     * @return mixed
     * @author alice
     * @date 2016-1-25
     */
    public function GetSpecInfo($id)
    {
        return $this->mSpec->GetSpecInfo($id);
    }

    /**
     * 添加规格
     * @param $paramObj
     * @return int
     * @author alice
     * @date 2016-1-25
     */
    public function AddSpec($paramObj)
    {
        $aData = [
            'spec_name'          =>     $paramObj->spec_name,
            'quality'            =>     $paramObj->quality,
        ];
        return $this->mSpec->insert($aData);

    }

    /**
     * 修改规格
     * @param $paramObj
     * @return bool
     * @author alice
     * @date 2016-1-25
     */
    public function EditSpec($paramObj)
    {
        $aData = [
            'spec_name'              =>     $paramObj->spec_name,
            'quality'                =>     $paramObj->quality,
        ];

        return $this->mSpec->update($aData,['spec_id'=>$paramObj->spec_id]);
    }

    /**
     * 展示等级列表
     * @return mixed
     * @author alice
     * @date 2016-1-25
     */
    public function ShowLevelList()
    {
        return $this->mLevel->ShowLevelList();
    }

    /**
     * 展示等级详情
     * @param $id
     * @return mixed
     * @author alice
     * @date 2016-1-25
     */
    public function GetLevelInfo($id)
    {
        return $this->mLevel->GetLevelInfo($id);
    }

    /**
     * 添加等级
     * @param $paramObj
     * @return int
     * @author alice
     * @date 2016-1-25
     */
    public function AddLevel($paramObj)
    {
        $aData = [
            'level_name'         =>     $paramObj->level_name,
            'quality'            =>     $paramObj->quality,
        ];
        return $this->mLevel->insert($aData);

    }

    /**
     * 修改等级
     * @param $paramObj
     * @return bool
     * @author alice
     * @date 2016-1-25
     */
    public function EditLevel($paramObj)
    {
        $aData = [
            'level_name'             =>     $paramObj->level_name,
            'quality'                =>     $paramObj->quality,
        ];
        return $this->mLevel->update($aData,['level_id'=>$paramObj->level_id]);
    }




}